﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Database;
using Common.Utilities.Dispatcher;
using Microsoft.SqlServer.Management.Smo;

namespace Workflows.Components.SchemaUtil
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CLRScripterEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private CLRScripterComponent _CLRScripterComponent;
        
        /// <summary>
        /// 
        /// </summary>
        public CLRScripterEditor()
        {
            InitializeComponent();
        }

        private void CLRScripterEditor_Load(object sender, EventArgs e)
        {
            // populate sql servers 
            this.cbo_Servers.Items.Clear();
            DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.cbo_Servers.Items.Add(dr["Name"].ToString());
                }
            }

            // populate auth types 
            this.cbo_AuthTypes.Items.Clear();
            for(int i=0;i<=1;i++)
            {
                AuthenticationType authType = (AuthenticationType) i;
                this.cbo_AuthTypes.Items.Add(authType.ToString());
            }
        }

        private void cbo_Servers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_Servers.SelectedIndex>=0)
            {
                string sqlServerInstanceName = this.cbo_Servers.SelectedItem.ToString();
                string[] dbNames = null;
                if(this.cbo_AuthTypes.SelectedIndex==0)
                {
                    string userName = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
                    dbNames = SqlServerEnumUtil.GetDatabases(sqlServerInstanceName, true, userName, "");
                }
                else
                {
                    dbNames = SqlServerEnumUtil.GetDatabases(sqlServerInstanceName, false, this.txt_UserName.Text,
                                                             this.txt_Password.Text);
                }
                this.cbo_Dbs.Items.Clear();
                if(dbNames !=null && dbNames.Length>0)
                {
                    foreach(string dbName in dbNames)
                    {
                        this.cbo_Dbs.Items.Add(dbName);
                    }
                }
            }
        }

        private void cbo_AuthTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbo_AuthTypes.SelectedIndex >= 0)
            {
                AuthenticationType authType = (AuthenticationType) this.cbo_AuthTypes.SelectedIndex;
                if(authType==AuthenticationType.WindowsAuthentication)
                {
                    this.txt_UserName.Text = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
                    this.txt_Password.Text = string.Empty;
                    this.txt_Password.Enabled = false;
                }
                else
                {
                    this.txt_Password.Enabled = true;
                }
            }
        }

        private void cbo_Dbs_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



        #region Implementation of IComponentEditor

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof(CLRScripterComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = OwnerActivityNode;
            this._CLRScripterComponent = (CLRScripterComponent) wfComponent;


        }

        /// <summary></summary>
        public void SaveSettings()
        {
            throw new System.NotImplementedException();
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion



    }
}
